import * as uniApi from './uni-api'

// 主题类型
export const ThemeType = {
	LIGHT: 'light',
	DARK: 'dark'
}

// 当前主题
let currentTheme = ThemeType.LIGHT

// 设置主题
export const setTheme = (theme) => {
	currentTheme = theme
	uniApi.setStorage('theme', theme)
	
	// 设置状态栏样式
	if (theme === ThemeType.DARK) {
		uni.setNavigationBarColor({
			frontColor: '#ffffff',
			backgroundColor: '#141414'
		})
	} else {
		uni.setNavigationBarColor({
			frontColor: '#000000',
			backgroundColor: '#ffffff'
		})
	}
}

// 获取当前主题
export const getTheme = () => currentTheme

// 切换主题
export const toggleTheme = () => {
	const newTheme = currentTheme === ThemeType.LIGHT ? ThemeType.DARK : ThemeType.LIGHT
	setTheme(newTheme)
	return newTheme
}

// 初始化主题
export const initTheme = () => {
	const savedTheme = uniApi.getStorage('theme')
	if (savedTheme) {
		setTheme(savedTheme)
	} else {
		setTheme(ThemeType.LIGHT)
	}
}

export default {
	ThemeType,
	setTheme,
	getTheme,
	toggleTheme,
	initTheme
} 